﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using Entities;
using Bussiness;

namespace ShoppingCartWEB
{
    public partial class ShoppingCart : System.Web.UI.Page
    {
        ShoppingCard card = new ShoppingCard();
        ProductManager man = new ProductManager();
        protected void Page_Load(object sender, EventArgs e)
        {

            card.Cards = GetCart();
            if (card.Cards.Count == 0)
            {
                btnPurchase.Visible = false;
                lblNote.Text = "Your cart is Empty!";
            }
            else
            {
                btnPurchase.Visible = true;
                if (!IsPostBack)
                {
                    RepShoppingCart.DataSource = card.Cards;
                    RepShoppingCart.DataBind();
                }
            }
            

        }

        private Dictionary<Product, int> GetCart()
        {
            if (Session["Cart"] == null)
                Session.Add("Cart", new Dictionary<Product, int>());
            return (Dictionary<Product, int>)Session["Cart"];
        }

        protected void RepShoppingCart_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "ShoppingCartContinue")
            {
                Response.Redirect("Products.aspx");
            }

            Response.Write(e.CommandArgument.ToString());

            if (e.CommandName == "UpdateShoppingCart")
            {
                int number;
                int id;
                decimal money;
                Label lblThanhTien;
                Product pr;
                foreach (RepeaterItem item in RepShoppingCart.Items)
                {
                    if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
                    {

                        try
                        {
                            number = Convert.ToInt32(((TextBox)item.FindControl("txtNumber")).Text);
                            id = int.Parse(((HiddenField)item.FindControl("hndProductId")).Value);
                            pr = man.GetProductById(id);
                            card.update(pr, number);
                            lblThanhTien = (Label)item.FindControl("lblMoney");
                            money = (decimal)pr.ProductPrice * number;
                            lblThanhTien.Text = money.ToString("0,0",
                  CultureInfo.InvariantCulture);
                        }
                        catch (Exception)
                        {

                            throw;
                        }
                    }
                }
                if (e.Item.ItemType == ListItemType.Footer)
                {
                    Label lblTongTien = (Label)e.Item.FindControl("lblTongTien");
                    if (lblTongTien != null)
                    {
                        lblTongTien.Text = card.Total.ToString("0,0",
                            CultureInfo.InvariantCulture);
                    }

                }


                //int id = Convert.ToInt32(e.CommandArgument.ToString());
                //string number = ((TextBox)e.Item.FindControl("txtNumber")).Text;
                //card.update(man.GetProductById(id), Convert.ToInt32(number));
                //RepShoppingCart.DataSource = card.Cards;
                //RepShoppingCart.DataBind();

            }

            if (e.CommandName == "DeleteItem")
            {
                if (e.CommandArgument != null)
                {
                    int id = Convert.ToInt32(e.CommandArgument.ToString());
                    Product p = man.GetProductById(id);
                    card.RemoveProduct(p);
                    RepShoppingCart.DataSource = card.Cards;
                    RepShoppingCart.DataBind();
                }

            }
        }

        protected void RepShoppingCart_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            KeyValuePair<Product, int> kvp;
            Label lblSTT = e.Item.FindControl("lblSTT") as Label;
            if (lblSTT != null)
            {
                kvp = (KeyValuePair<Product, int>)e.Item.DataItem;
                lblSTT.Text = kvp.Key.ProductID.ToString();
                lblSTT.Text = e.Item.ID;
            }
            

            Image img = e.Item.FindControl("imgProduct") as Image;
            if (img != null)
            {
                kvp = (KeyValuePair<Product, int>)e.Item.DataItem;
                img.ImageUrl = kvp.Key.imageLink;
            }

            Label lblName = e.Item.FindControl("lblProductName") as Label;
            if (lblName != null)
            {
                kvp = (KeyValuePair<Product, int>)e.Item.DataItem;
                lblName.Text = kvp.Key.ProductName.ToString();
            }

            Label lblPrice = e.Item.FindControl("lblPrice") as Label;
            if (lblPrice != null)
            {
                kvp = (KeyValuePair<Product, int>)e.Item.DataItem;
                lblPrice.Text = ((decimal)kvp.Key.ProductPrice).ToString("0,0", CultureInfo.InvariantCulture);
            }

            TextBox txtNumber = e.Item.FindControl("txtNumber") as TextBox;
            if (txtNumber != null)
            {
                kvp = (KeyValuePair<Product, int>)e.Item.DataItem;
                txtNumber.Text = kvp.Value.ToString();
            }

            Label lblMoney = e.Item.FindControl("lblMoney") as Label;
            if (lblMoney != null)
            {
                kvp = (KeyValuePair<Product, int>)e.Item.DataItem;
                decimal money = (decimal)(kvp.Value * kvp.Key.ProductPrice);
                lblMoney.Text = money.ToString("0,0", CultureInfo.InvariantCulture);
            }

            if (e.Item.ItemType == ListItemType.Footer)
            {
                Label lblTongTien = e.Item.FindControl("lblTongTien") as Label;
                if (lblTongTien != null)
                {
                    lblTongTien.Text = card.Total.ToString("0,0",
                  CultureInfo.InvariantCulture);
                }

            }
        }

        protected void RepShoppingCart_DataBinding(object sender, EventArgs e)
        {

        }

        protected void btnPurchase_Click(object sender, EventArgs e)
        {
            Response.Redirect("Customer1.aspx");
        }
    }
}